package com.admin.education.services.Implements;

import com.admin.education.common.exceptions.ResourceNotFoundException;
import com.admin.education.dataTransferObject.PageResponse;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.Sort;
import com.admin.education.dataTransferObject.integrals.IntegralsTransfer;
import com.admin.education.dataTransferObject.integrals.Mapper.IntegralsMapper;
import com.admin.education.dataTransferObject.integrals.Request.QueryIntegralRequest;
import com.admin.education.dataTransferObject.userInformation.UserInformationTransfer;
import com.admin.education.models.Integrals;
import com.admin.education.models.QGrades;
import com.admin.education.models.QIntegrals;
import com.admin.education.models.QUserInformation;
import com.admin.education.repositories.IntegralsRepository;
import com.admin.education.services.Interfaces.IIntegralsService;
import com.querydsl.jpa.impl.JPAQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import java.util.List;

@Service
@Validated
public class IntegralsService implements IIntegralsService {
    @Autowired
    private IntegralsMapper integralsMapper;
    @Autowired
    private IntegralsRepository integralsRepository;
    private QIntegrals qIntegrals;
    private QGrades qGrades;
    private QUserInformation qUserInformation;

    public IntegralsService() {
        qIntegrals = QIntegrals.integrals;
        qGrades = QGrades.grades;
        qUserInformation = QUserInformation.userInformation;
    }

    @Override
    public ServicesExcuteResult<PageResponse<IntegralsTransfer>> queryIntegrals(@Valid QueryIntegralRequest queryIntegralRequest) {
        ServicesExcuteResult<PageResponse<IntegralsTransfer>> servicesExcuteResult = new ServicesExcuteResult<>();
        JPAQuery<Integrals> integralsJPAQuery = integralsRepository.selectFrom(qIntegrals);
        if (null != queryIntegralRequest.getSchoolsId()) {
            integralsJPAQuery = integralsJPAQuery.where(qIntegrals.userInformation.schools.id.eq(queryIntegralRequest.getSchoolsId()));
        }
        if (null != queryIntegralRequest.getClassesId()) {
            integralsJPAQuery = integralsJPAQuery.where(qIntegrals.userInformation.classes.id.eq(queryIntegralRequest.getClassesId()));
        }
        if (null != queryIntegralRequest.getGradeId()) {
            integralsJPAQuery = integralsJPAQuery.innerJoin(qIntegrals.userInformation,qUserInformation).where(qUserInformation.classes.grades.id.eq(queryIntegralRequest.getGradeId()));
        }

        long totalCount = integralsJPAQuery.fetchCount();

        if ("value".equals(queryIntegralRequest.getSort())) {
            integralsJPAQuery = integralsJPAQuery.orderBy(qIntegrals.value.asc());
        } else if ("fans".equals(queryIntegralRequest.getSort())) {
            integralsJPAQuery = integralsJPAQuery.orderBy(qUserInformation.fans.size().desc());
        } else {
            integralsJPAQuery = integralsJPAQuery.orderBy(qIntegrals.value.desc());
        }

        integralsJPAQuery = integralsJPAQuery.offset((queryIntegralRequest.getPageIndex() - 1) * queryIntegralRequest.getPageSize()).limit(queryIntegralRequest.getPageSize());
        List<Integrals> integrals = integralsJPAQuery.fetch();

        List<IntegralsTransfer> integralsTransfers = integralsMapper.mapFrom(integrals);

        PageResponse<IntegralsTransfer> integralsTransferPageResponse = new PageResponse<IntegralsTransfer>(queryIntegralRequest.getPageSize(), queryIntegralRequest.getPageIndex(), (int) totalCount, integralsTransfers);

        servicesExcuteResult.setData(integralsTransferPageResponse);
        servicesExcuteResult.setSuccess(true);
        return servicesExcuteResult;
    }


    @Override
    public ServicesExcuteResult<IntegralsTransfer> queryIntegralByUserInformationId(long userInformationId) throws ResourceNotFoundException {
        ServicesExcuteResult<IntegralsTransfer> servicesExcuteResult = new ServicesExcuteResult<>();
        JPAQuery<Integrals> integralsJPAQuery = integralsRepository.selectFrom(qIntegrals).where(qIntegrals.userInformation.id.eq(userInformationId));
        if (0 != integralsJPAQuery.fetchCount()) {
            Integrals integrals = integralsJPAQuery.fetchFirst();

            servicesExcuteResult.setData(integralsMapper.mapFrom(integrals));
            servicesExcuteResult.setSuccess(true);
        } else {
            throw new ResourceNotFoundException("不存在该用户的积分");
        }
        return servicesExcuteResult;
    }
}
